﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Draco.OWF.RT.Engine.Manager
{
    [XmlRoot("Manifest")]
    public class Manifest
    {
        [XmlElement("PluginName",Order = 1)]
        public String PluginName { get; set; }
        [XmlElement("Version", Order =2)]
        public String Version { get; set; }
        [XmlElement("DispalyName",Order = 3)]
        public String DispalyName { get; set; }
        [XmlElement("AssemblyName", Order = 4)]
        public String AssemblyName { get; set; }
        [XmlArray("Activities",Order = 5)]
        [XmlArrayItem("Activity")]
        public List<ActivityManifest> ActivityManifests { get; set; }

        public void Load(System.Xml.XmlDocument xDoc)
        {
            XmlElement xel = xDoc.DocumentElement;
            this.PluginName = Utils.XmlUtils.getChildValue(xel, "PluginName");
            this.Version = Utils.XmlUtils.getChildValue(xel, "Version");
            this.DispalyName = Utils.XmlUtils.getChildValue(xel, "DispalyName");
            this.AssemblyName = Utils.XmlUtils.getChildValue(xel, "AssemblyName");

            XmlElement xActivities = Utils.XmlUtils.getChildElement(xel,"Activities");
            XmlNodeList list = xActivities.SelectNodes("Activity");
            foreach(XmlNode n in list)
            {
                XmlElement ael = n as XmlElement;
                ActivityManifest activity = new ActivityManifest();
                activity.Load(this,ael);
                ActivityManifests.Add(activity);
            }
        }
    }

    [XmlType(TypeName = "Activity")]
    public class ActivityManifest
    {
        [XmlElement("Name")]
        public String Name { get; set; }
        [XmlElement("ClassName")]
        public String ClassName { get; set; }

        [XmlIgnore]
        public Manifest Manifest { get; set; }

        public void Load(Manifest manifest,XmlElement xElement)
        {
            this.Manifest = manifest;
            this.Name = Utils.XmlUtils.getChildValue(xElement, "Name");
            this.ClassName = Utils.XmlUtils.getChildValue(xElement, "ClassName");
        }
    }
}
